package com.xiaomi.web.handlers;

import com.github.pagehelper.PageInfo;
import com.xiaomi.domain.CategoryEntity;
import com.xiaomi.domain.GoodsEntity;
import com.xiaomi.service.CateService;
import com.xiaomi.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexHandler {
    @Autowired
    private CateService cateService;
    @Autowired
    private GoodsService goodsService;
    @RequestMapping(name = "获取商品分类和商品信息",value = "/index1")
    public  String index(HttpServletRequest request){
        //获取10个cate信息
        PageInfo<CategoryEntity> cates=null;
        List<GoodsEntity> goodsEntities=null;
        List<GoodsEntity> goods1=new ArrayList<GoodsEntity>();
        List<GoodsEntity> goods2=new ArrayList<GoodsEntity>();
        List<GoodsEntity> goods3=new ArrayList<GoodsEntity>();
        List<GoodsEntity> goods4=new ArrayList<GoodsEntity>();
        List<GoodsEntity> goods5=new ArrayList<GoodsEntity>();
        try {
            cates=cateService.findAllCates(1,10);
            goodsEntities=goodsService.findAllGodds();

        } catch (IOException e) {
            e.printStackTrace();
        }
        int i=1;
        List<CategoryEntity> categoryEntityList=cates.getList();
        for(CategoryEntity cate:categoryEntityList){
            for(GoodsEntity good:goodsEntities){
                if(good.getCategory().getCid()==cate.getCid()){
                    if(i==1){
                        goods1.add(good);
                    }
                    if(i==2){
                        goods2.add(good);
                    }
                    if(i==3){
                        goods3.add(good);
                    }
                    if(i==4){
                        goods4.add(good);
                    }
                    if(i==5){
                        goods5.add(good);
                    }
                }

            }
            i++;
        }


        HttpSession session=request.getSession();
        session.setAttribute("cates",categoryEntityList);
        session.setAttribute("goods1",goods1);
        session.setAttribute("goods2",goods2);
        session.setAttribute("goods3",goods3);
        session.setAttribute("goods4",goods4);
        session.setAttribute("goods5",goods5);


        return "index";
    }
}
